//
//  Date.m
//  Date
//
//  Created by SongYakun on 15/12/29.
//  Copyright © 2015年 SongYakun. All rights reserved.
//

#import "Date.h"

@implementation Date

+ (NSString *)changeDateString:(NSString *)dateStr {
    //设置时间格式
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyy-MM-dd";
    NSDate * stringDate = [formatter dateFromString:dateStr];
    NSDate * nowStrDate = [stringDate dateByAddingTimeInterval:8 * 60 * 60];
    NSLog(@"nowStrDate----%@",nowStrDate);
    
    NSDate * date = [NSDate date];
    //获取当前时间
    NSDate * nowDate = [date dateByAddingTimeInterval:8 * 60 * 60];
    NSLog(@"nowDate----%@",nowDate);
    NSString * nowDateStr = [formatter stringFromDate:date];
    NSLog(@"nowDateStr----%@",nowDateStr);
    if ([dateStr isEqualToString:nowDateStr]) {
//        return @"今天";
    }
    //时间差
    double nowInterval = [nowDate timeIntervalSinceReferenceDate];
    double strInterval = [nowStrDate timeIntervalSinceReferenceDate];
    NSLog(@"strInterval-----%f", strInterval);
    NSLog(@"nowInterval----%f",nowInterval);
    NSLog(@"%f",(nowInterval - strInterval));
    if ((nowInterval - strInterval) >= 0) {
        if ((nowInterval - strInterval) <= 24 * 60 * 60) {
            return @"今天";
        }else if (24 * 60 * 60 < nowInterval - strInterval && nowInterval - strInterval <= 24 * 60 * 60 * 2) {
            return @"昨天";
        }else if (24 * 60 * 60 * 2 < nowInterval - strInterval && nowInterval - strInterval <= 24 * 60 * 60 * 3) {
            return @"前天";
        }else {
            double interval = (nowInterval - strInterval) / (24 * 60 * 60);
            NSString * str = [NSString stringWithFormat:@"%d天前",(int)interval];
            return str;
        }
    }else {
        if ((strInterval - nowInterval) <= 24 * 60 * 60) {
            return @"今天";
        }else if (24 * 60 * 60 < strInterval - nowInterval && strInterval - nowInterval <= 24 * 60 * 60 * 2) {
            return @"明天";
        }else if (24 * 60 * 60 * 2 < strInterval - nowInterval && strInterval - nowInterval <= 24 * 60 * 60 * 3) {
            return @"后天";
        }else {
            double interval = (strInterval - nowInterval) / (24 * 60 * 60);
            NSString * str = [NSString stringWithFormat:@"%d天后",(int)interval];
            return str;
        }
    }
    
    
    
    return nil;
}

@end
